package yl.hs.bmipfull.controllers.report;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.pojo.production.BrickShipOrderModel;
import yl.hs.bmipfull.pojo.production.FinishProductTrayEntity;
import yl.hs.bmipfull.pojo.production.FinishProductWarehouseRecordEntity;
import yl.hs.bmipfull.service.production.IBrickShipService;
import yl.hs.bmipfull.service.production.IFinishProductOutboundRecordService;
import yl.hs.bmipfull.service.production.IFinishProductTrayService;
import yl.hs.bmipfull.service.production.IFinishProductWarehouseRecordService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.WebResult;
import yl.hs.bmipfull.viewmodels.statistics.MonthlySalesReportModel;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Comparator;
import java.util.stream.Collectors;

/**
 * 作用:   <br/>
 * 创建时间: 2023-08-06 15:37 <br/>
 *
 * @author 吴高丰
 */
@Api(value = "化肥报表",tags = "报表中心-化肥报表")
@RestController
@RequestMapping(value = "/api/hf/Report")
public class HFReportController extends ControllerBase {
    @Autowired
    IBrickShipService brickShipService;
    @Autowired
    IFinishProductWarehouseRecordService WarehouseRecord;
    @Autowired
    IFinishProductTrayService productTrayService;

    public WebResult SalesMonthlyReport(@RequestBody PageQuery query){
        try {
            var result = new MonthlySalesReportModel();
            var ship = brickShipService.queryList(query);
            var tray = productTrayService.queryList(query);
            query.getQuery().put("type", 0);
            var outbaund =  WarehouseRecord.queryList(query);
            var productSN =  ship.stream().map(BrickShipOrderModel::getProductSN).toList();
            var productSNs =  ship.stream().map(BrickShipOrderModel::getProductSN).toList();
            productSN.addAll(productSNs);
            var productsn = productSN.stream().distinct().collect(Collectors.toList());
            for (String s : productsn) {
                var dd = ship.stream().filter(d->d.getProductSN().equals(s));
                var ss = outbaund.stream().filter(d->d.getProductSN().equals(s));
                result.setCate(dd!=null?dd.toList().get(0).getProductCate():ss.toList().get(0).getProductCateName());
                result.setName(dd!=null?dd.toList().get(0).getProductName():ss.toList().get(0).getProductName());
                result.setSpace(dd!=null?dd.toList().get(0).getProductSpec():ss.toList().get(0).getProductSpec());
                result.setScRk(ss==null? BigDecimal.valueOf(0) :ss.toList().stream().map(FinishProductWarehouseRecordEntity::getQuantity).reduce(BigDecimal::add).get());
                result.setXsCk(dd==null? BigDecimal.valueOf(0) :dd.toList().stream().map(BrickShipOrderModel::getQuantity).reduce(BigDecimal::add).get());
                if(tray!=null){
                    tray.sort(Comparator.comparing(FinishProductTrayEntity::getAddTime));
                    result.setCqKc(tray.stream().findFirst().get().getNewQuantity());
                    //result.
                }else{
                    result.setCqKc(BigDecimal.valueOf(0));
                }
                //result.setCqKc(tray.stream().);
            }
        }catch (Exception ee){

        }
       return null;
    };
}
